package yicr.panne;



import yicr.Form;
import yicr.GBC;
import yicr.MainFrame;
import yicr.untils.MaterialAuditServiceImpl;
import yicr.untils.NslUtilCsv;
import yicr.untils.NslUtilFile;
import yicr.untils.NslUtilFilePath;
import yicr.untils.datetime.NslUtilDateTime;
import yicr.untils.gis.Polygon.NslPolygon;
import yicr.untils.gis.Polygon.NslUtilPolygon;
import yicr.untils.gis.geoHash.GeoHash;
import yicr.untils.gis.geoHash.NslUtilGeoHash;
import yicr.untils.gis.mif.UtilMapInfoMif;
import yicr.untils.gis.wireless.NslUtilWireless;
import yicr.untils.gis.wireless.WirelessCell;

import javax.swing.*;
import javax.swing.border.Border;
import javax.swing.filechooser.FileFilter;
import javax.swing.filechooser.FileNameExtensionFilter;
import java.awt.*;
import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
 * <h3>Title:</h3>
 * <p>
 *
 * <h3>Description</h3>
 * 面板分为2个层次：左1，右1，左1为 参数和按钮区域，右1为图片区域
 * 左1： 再分为上下两个区域，上为参数设置区域，下为按钮区域
 *
 * <h3>Author</h3>
 * <p>
 * Lin Shao
 * <h3>Update Time</h3>
 * <p>2021-10-23 20:59
 *
 * @author Lin Shao
 * @version 1.0
 */
public class SectorScanningPane extends JPanel {

    //确保所有点都能被扫到
    private int cellRadius=350;

    private Form form=null;
    private int geohashColumnIndex;

    private static final int DEFAULT_WIDTH=740;
    private static final int DEFAULT_HEIGHT=340;


    private JLabel tipLabel1 =null;
    private JLabel tipLabel2 = null;
    private JLabel tipLabel3 =null;
    private JLabel tipLabel4 =null;


    private JTextField textField1 =null;
    private JTextField textField2 =null;
    private JTextField textField3 =null;
    private JTextField textField4 =null;

    private JLabel unitLabel1 =new JLabel(" ");
    private JLabel unitLabel2 =new JLabel(" ");
    private JLabel unitLabel3 =new JLabel(" ");
    private JLabel unitLabel4 =new JLabel("");

    private String defaultTextField1 ="";
    private String defaultTextField2 ="";
    private String defaultTextField3 ="";
    private String defaultTextField4 ="";



    private JButton importDataButton=null;
    private JButton calDataButton=null;

    private JFileChooser fileChooser=null;
    private String importFileAbsPath;

    private List<String> lineList =null;

    private double multiplicationOfSize=1;

    private void setMultiplicationOfSize(){
        Toolkit kit = Toolkit.getDefaultToolkit(); // 定义工具包
        Dimension screenSize = kit.getScreenSize(); // 获取屏幕的尺寸
        multiplicationOfSize=screenSize.width/1920;
    }

    public SectorScanningPane() throws URISyntaxException {
        setMultiplicationOfSize();
        initFont();

        lineList =new ArrayList<>();

        setSize(new Dimension((int) (DEFAULT_WIDTH*multiplicationOfSize), (int) (DEFAULT_HEIGHT*multiplicationOfSize)));

        fileChooser=new JFileChooser();

        FileFilter filter = new FileNameExtensionFilter(
                "Csv files", "csv");
        fileChooser.setFileFilter(filter);


        String basePath=System.getProperty("exe.path");



        String exePath=new File("").getAbsolutePath()+"\\";





//      tipLabel1 =new JLabel(new String(basePath.getBytes(),"GB2312"));
        tipLabel1 =new JLabel("审定表文件位置:");

        //new String(value.getBytes("ISO8859-1"), encoding)
        tipLabel2 =new JLabel("OA导出材料表位置");
        //longitudeLabel = new JLabel(new String(basePath.getBytes("ISO8859-1"),"GBK"));

        tipLabel3 =new JLabel("统一对照表位置:");
        //latitudeLabel=new JLabel(new String(new File("").getAbsolutePath()));
        tipLabel4 =new JLabel("合同编号");

        //geoHashBitSezeLabel=new JLabel(new String(new File("/").getAbsolutePath()));


        textField1 =new JTextField();
        textField2 =new JTextField();
        textField3 =new JTextField();
        textField4 =new JTextField();

        int textColumn=40;

        textField1.setColumns(textColumn);
        textField2.setColumns(textColumn);
        textField3.setColumns(textColumn);
        textField4.setColumns(textColumn);



        importDataButton=new JButton("材料核对");
        calDataButton=new JButton("扫描Mif&Tab导出");



        textField1.setHorizontalAlignment(JTextField.CENTER);
        textField2.setHorizontalAlignment(JTextField.CENTER);
        textField3.setHorizontalAlignment(JTextField.CENTER);
        textField4.setHorizontalAlignment(JTextField.CENTER);

        textField1.setText(defaultTextField1+"");
        textField2.setText(defaultTextField2 +"");
        textField3.setText(defaultTextField3 +"");
        textField4.setText(defaultTextField4 +"");

//        String imageFilePath="\\geohash_2_f.png";

        String imageAbsPath="d:/antenna_1.png";

        imageAbsPath=Thread.currentThread().getContextClassLoader().getResource("")+"\\geohash_2_f.png";
        //imageAbsPath=Thread.currentThread().getContextClassLoader().getResource("")+"\\antenna_1.png";
        imageAbsPath=imageAbsPath.replace("file:","");

//        imageAbsPath= MainFrame.calResourceAbsPath(MainFrame.whetherLocalRunning,imageAbsPath);


        //System.out.println(new File(imageAbsPath).getAbsolutePath());
        JLabel imageLabel=new JLabel(new ImageIcon(imageAbsPath));
        Border etched = BorderFactory.createEtchedBorder();
        imageLabel.setBorder(etched);

        JPanel parameterPanel=new JPanel();

        GridBagLayout gridBagLayoutOfParameterPanel=new GridBagLayout();

        parameterPanel.setLayout(gridBagLayoutOfParameterPanel);

        int top=20;
        int bottom=20;

        parameterPanel.add(tipLabel1,new GBC(0,0,1,1).setAnchor(GridBagConstraints.EAST).setInsets(top,30,15,20));
        parameterPanel.add(tipLabel2,new GBC(0,1,1,1).setAnchor(GridBagConstraints.EAST).setInsets(15,30,15,20));
        parameterPanel.add(tipLabel3,new GBC(0,2,1,1).setAnchor(GridBagConstraints.EAST).setInsets(15,30,15,20));
//        parameterPanel.add(tipLabel4,new GBC(0,3,1,1).setAnchor(GridBagConstraints.EAST).setInsets(15,30,bottom,20));

        parameterPanel.add(textField1,new GBC(1,0,1,1).setAnchor(GridBagConstraints.EAST).setInsets(top,0,15,0));
        parameterPanel.add(textField2,new GBC(1,1,1,1).setAnchor(GridBagConstraints.EAST).setInsets(15,0,15,0));
        parameterPanel.add(textField3,new GBC(1,2,1,1).setAnchor(GridBagConstraints.EAST).setInsets(15,0,15,0));
//        parameterPanel.add(textField4,new GBC(1,3,1,1).setAnchor(GridBagConstraints.EAST).setInsets(15,0,bottom,0));

        parameterPanel.add(unitLabel1,new GBC(2,0,1,1).setAnchor(GridBagConstraints.WEST).setInsets(top,5,15,10));
        parameterPanel.add(unitLabel2,new GBC(2,1,1,1).setAnchor(GridBagConstraints.WEST).setInsets(15,5,15,10));
        parameterPanel.add(unitLabel3,new GBC(2,2,1,1).setAnchor(GridBagConstraints.WEST).setInsets(15,5,15,10));
//        parameterPanel.add(unitLabel4,new GBC(2,3,1,1).setAnchor(GridBagConstraints.WEST).setInsets(15,5,bottom,10));

        //Border etched = BorderFactory.createEtchedBorder();
//        Border titled = BorderFactory.createTitledBorder(etched, "扇区扫描参数设置");

//        parameterPanel.setBorder(titled);


        GridBagLayout mainGridBagLayout=new GridBagLayout();

        setLayout(mainGridBagLayout);

        JPanel buttonPanel=new JPanel();
        buttonPanel.add(importDataButton, new GBC(0,0,1,1).setAnchor(GridBagConstraints.CENTER).setInsets(10));


        add(parameterPanel,new GBC(0,0,2,1));

        add(buttonPanel,new GBC(0,1,2,1).setInsets(15));

        add(imageLabel,new GBC(2,0,1,2).setInsets(15,30,15,15));


        setScreenCenter();
        //pack();

        addEvents();
    }

    private void addEvents() {

        importDataButton.addActionListener(e -> {
            String auditMaterialExcelPath = textField1.getText();
            String oaMaterialExcelPath = textField2.getText();
            String unifiedRecordExcelPath = textField3.getText();
            String contractSn = textField4.getText();

            File auditMaterialExcelFile = new File(auditMaterialExcelPath);
            File  oaMaterialExcelFile= new File(oaMaterialExcelPath);
            File unifiedRecordExcelFile= new File(unifiedRecordExcelPath);
            try {
                System.out.println(NslUtilFilePath.getAbsPathBaseOnClassPath("material_info_fill_template_v3.xlsx"));
            } catch (URISyntaxException ex) {
                throw new RuntimeException(ex);
            }

            if(!auditMaterialExcelFile.exists() || !oaMaterialExcelFile.exists() || !unifiedRecordExcelFile.exists()){
                throw new RuntimeException("输入文件目录不正确");
            }

//            if(contractSn==null || contractSn.trim().length()==0){
//                throw new RuntimeException("合同编号不正确");
//            }
            String auditMaterialExcelSheetName="定案表";
            String oaMaterialExcelSheetName="项目物资核算详细导出";
            String unifiedRecordExcelSheetName="统一名称规格_n";

            List<String> auditMaterialExcelPathList =new ArrayList<>();
            auditMaterialExcelPathList.add(auditMaterialExcelPath);

            List<String> contractSnList=new ArrayList<>();
            contractSnList.add(contractSn);

            String compareExcelTemplatePath="D:\\Java_Projects\\ideaIU-2022.2.3.win_projects\\yicr_supply\\yicr-supply-audit\\yicr-supply-audit-v1\\src\\main\\resources\\material_info_fill_template_v3.xlsx";
            //根据 resourceDir 是否为null,判断是在开发模式还是exe模式
            String resourceDir = System.getProperty("resource_dir");
            if(resourceDir!=null){
                String resourcePath = resourceDir + "/template.data";
                compareExcelTemplatePath=resourcePath;
            }else{
                try {
                    compareExcelTemplatePath=NslUtilFilePath.getAbsPathBaseOnClassPath("template.data");
                } catch (URISyntaxException ex) {
                    throw new RuntimeException(ex);
                }
            }




            String outputExcelFilePath=auditMaterialExcelFile.getParent()+"\\"+NslUtilDateTime.getDateStringAsDd_Hh_Mm_SS()+".xlsx";



            MaterialAuditServiceImpl.fillMaterialInfoCompareExcelTemplateV2(auditMaterialExcelPathList,auditMaterialExcelSheetName,oaMaterialExcelPath,oaMaterialExcelSheetName,unifiedRecordExcelPath,unifiedRecordExcelSheetName,compareExcelTemplatePath,outputExcelFilePath);




            JOptionPane.showMessageDialog(null, "已在定案表目录下，生成核对表。");
        });

    }


    private List<WirelessCell> lineList2WirelessCellList(List<String> lineList,int sectorAngle){
        List<WirelessCell> cellList=new ArrayList<>();
        for (int i = 1; i < lineList.size(); i++) {
            String line=lineList.get(i);
            String[] stringArray=line.split(",");
            String id=stringArray[0];
            double longitude=Double.parseDouble(stringArray[1]);
            double latitude=Double.parseDouble(stringArray[2]);
            double angle=Double.parseDouble(stringArray[3]);

            WirelessCell cell=new WirelessCell();
            cell.setId(id);
            cell.setLongitude(longitude);
            cell.setLatitude(latitude);
            cell.setDirectionAngle(angle);
            cell.setHorizontalBeam(sectorAngle);
            cell.setCoverRadius(cellRadius);

            cellList.add(cell);
        }
        return cellList;
    }


    private void initFont(){
        Font f = new Font("宋体", Font.PLAIN, (int) (12*multiplicationOfSize));

        String names[]={ "Label", "CheckBox", "PopupMenu","MenuItem", "CheckBoxMenuItem",
                "JRadioButtonMenuItem","ComboBox", "Button", "Tree", "ScrollPane",
                "TabbedPane", "EditorPane", "TitledBorder", "Menu", "TextArea",
                "OptionPane", "MenuBar", "ToolBar", "ToggleButton", "ToolTip",
                "ProgressBar", "TableHeader", "Panel", "List", "ColorChooser",
                "PasswordField","TextField", "Table", "Label", "Viewport",
                "RadioButtonMenuItem","RadioButton", "DesktopPane", "InternalFrame"
        };
        for (String item : names) {
            UIManager.put(item+ ".font",f);
        }
    }

    private void setScreenCenter(){

        int windowWidth = getWidth(); // 获得窗口宽
        int windowHeight = getHeight(); // 获得窗口高
        Toolkit kit = Toolkit.getDefaultToolkit(); // 定义工具包
        Dimension screenSize = kit.getScreenSize(); // 获取屏幕的尺寸
        int screenWidth = screenSize.width; // 获取屏幕的宽
        int screenHeight = screenSize.height; // 获取屏幕的高
        setLocation(screenWidth / 2 - windowWidth / 2, screenHeight / 2 - windowHeight / 2);// 设置窗口居中显示

    }



}
